﻿using System.Windows;
using System.Windows.Input;
using Red.Core.GoogleAnalytics;

namespace Red.Launcher.Dialogs
{
    public partial class WaitForProcessShutdownDialog
    {
        #region Data

        readonly string _analyticsCategory = typeof( WaitForProcessShutdownDialog ).Name;

        #endregion Data

        #region C-Tor

        partial void InitializeAnalytics()
        {
            CommandBindings[ 0 ].Executed += RaiseProcessAnalytics_Executed;
            CommandBindings[ 1 ].Executed += CloseProcessAnalytics_Executed;
            CommandBindings[ 2 ].Executed += ForceCloseAllAnalytics_Executed;
            CommandBindings[ 3 ].Executed += CancelAnalytics_Executed;

            Closed += WaitForProcessShutdownDialogAnalytics_Closed;
            Activated += WaitForProcessShutdownDialog_Activated;
        }

        #endregion C-Tor

        #region Event Handlers

        private void RaiseProcessAnalytics_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            ProcessWatcher processWatcher = (ProcessWatcher)( (FrameworkElement)e.OriginalSource ).DataContext;
            GoogleAnalyticsService.Instance.SendEvent( ( (RoutedUICommand)e.Command ).Name, _analyticsCategory, processWatcher.Process.ProcessName );
        }

        private void CloseProcessAnalytics_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            ProcessWatcher processWatcher = (ProcessWatcher)( (FrameworkElement)e.OriginalSource ).DataContext;
            GoogleAnalyticsService.Instance.SendEvent( ( (RoutedUICommand)e.Command ).Name, _analyticsCategory, processWatcher.Process.ProcessName );
        }

        private void ForceCloseAllAnalytics_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            GoogleAnalyticsService.Instance.SendEvent( ( (RoutedUICommand)e.Command ).Name, _analyticsCategory );
        }

        private void CancelAnalytics_Executed( object sender, ExecutedRoutedEventArgs e )
        {
            GoogleAnalyticsService.Instance.SendEvent( ( (RoutedUICommand)e.Command ).Name, _analyticsCategory );
        }

        private void WaitForProcessShutdownDialogAnalytics_Closed( object sender, System.EventArgs e )
        {
            GoogleAnalyticsService.Instance.SendEvent( "Window Closed", _analyticsCategory );

            CommandBindings[ 0 ].Executed -= RaiseProcessAnalytics_Executed;
            CommandBindings[ 1 ].Executed -= CloseProcessAnalytics_Executed;
            CommandBindings[ 2 ].Executed -= ForceCloseAllAnalytics_Executed;
            CommandBindings[ 3 ].Executed -= CancelAnalytics_Executed;

            Closed -= WaitForProcessShutdownDialogAnalytics_Closed;
        }

        private void WaitForProcessShutdownDialog_Activated( object sender, System.EventArgs e )
        {
            GoogleAnalyticsService.Instance.SetScreen( "Wait for process shutdown dialog" );
        }

        #endregion Event Handlers
    }
}
